package com.yms.yms.config;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yms.yms.dao.entity.YmsUserRole;
import com.yms.yms.dao.mapper.YmsRoleMapper;
import com.yms.yms.dao.mapper.YmsUserMapper;
import com.yms.yms.dao.mapper.YmsUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    YmsUserMapper ymsUserMapper;
    @Autowired
    YmsRoleMapper ymsRoleMapper;
    @Autowired
    YmsUserRoleMapper ymsUserRoleMapper;

    @Override
    public List<String> getPermissionList(Object id, String loginType) {
        return null;
    }

    @Override
    public List<String> getRoleList(Object id, String loginType) {
        // Todo 等DAO层成型，重构为使用DAO函数的实现
        QueryWrapper<YmsUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", id);
        List<YmsUserRole> userRoleList = ymsUserRoleMapper.selectList(wrapper);
        List<String> roles = new ArrayList<>();
        userRoleList.stream().map(YmsUserRole::getRid)
                .forEach(rid -> roles.add(String.valueOf(ymsRoleMapper.selectById(rid))));
        return roles;
    }
}
